'''
Created on 15/07/2010

@author: Mar
'''
'''
Created on 10/07/2010

@author: Mar
'''
import unittest
from multiprocessing.queues import Queue
from planifCortoPLazo_RoundRobin import RoundRobin
from ManagerResource import ResourceManager
from ManagerCPU import CPUManager
from OS import OS
from Program import Program
from CPUInstruction import CPUInstruction

import time


class RoundRobinTest(unittest.TestCase):


    def setUp(self):
        self.outputQueue = Queue()
        self.scheduler = RoundRobin(3)
        self.rm = ResourceManager()
        self.rm.registerResource("kb", 1, self.scheduler.getReadyQueue(), self.outputQueue)
        self.rm.registerResource("screen", 1, self.scheduler.getReadyQueue(), self.outputQueue)
        self.rm.registerResource("printer", 1, self.scheduler.getReadyQueue(), self.outputQueue)
        self.cpuManager = CPUManager(1, self.rm,self.outputQueue,self.scheduler)
        self.myOS = OS(self.scheduler, self.rm,self.cpuManager, self.outputQueue)
        self.myProg = Program()
        self.myProg2 = Program()
        self.in1=CPUInstruction(2,"cpu prog1")
        self.in2=CPUInstruction(2,"cpu prog")
        self.myProg.addInstruction(self.in1)
        self.myProg.addInstruction(self.in1)
        self.myProg.addInstruction(self.in1)
        self.myProg2.addInstruction(self.in2)
        self.myProg2.addInstruction(self.in2)
        self.myProg2.addInstruction(self.in2)
        self.myOS.addProgram(self.myProg)
        self.myOS.addProgram(self.myProg2)
        


    


    def testRoundRobin(self):
        self.queue = Queue()
        self.queue.put((self.in1.getWord(),self.in1.getTime()))
        self.queue.put((self.in2.getWord(),self.in2.getTime()))
        self.queue.put((self.in1.getWord(),self.in1.getTime()))
        self.queue.put((self.in2.getWord(),self.in2.getTime()))
        self.queue.put((self.in1.getWord(),self.in1.getTime()))
        self.queue.put((self.in2.getWord(),self.in2.getTime()))
        self.myOS.startUP()
        time.sleep(15)
        #self.myOS.shutDown()
        
        self.assertTrue(self.outputQueue.qsize() == 6)
        self.assertEqual(self.outputQueue.get(), self.queue.get())
        self.assertEqual(self.outputQueue.get(), self.queue.get())
        self.assertEqual(self.outputQueue.get(), self.queue.get())
        self.assertEqual(self.outputQueue.get(), self.queue.get())
        self.assertEqual(self.outputQueue.get(), self.queue.get())
        self.assertEqual(self.outputQueue.get(), self.queue.get())



if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
    
    
    

suite = unittest.TestLoader().loadTestsFromTestCase(RoundRobinTest)
unittest.TextTestRunner(verbosity=2).run(suite)